-- EggsM
-- Create by zouyb
-- 彩蛋模块

module("EggsM", package.seeall);

-- 所有的彩蛋配置信息
local eggsTable = {};

-- 当前彩蛋信息
local eggsInfo = {};

-- 载入配置表
local _init = false;
local loadCsv = function()
    if _init then return end
    _init = true;

    eggsInfo = {};
    -- 转换下audio信息表
    local eggs_CSV = get_eggs_CSV();
    for i = 1, #eggs_CSV do
        eggsTable[eggs_CSV[i].id] = parseInfo(eggs_CSV[i]);
    end

    eggs_CSV = {};
end

-- 模块初始化
function init()
    loadCsv();
end

-- 解析坐标
function parseInfo(info)
    local function parsePosStr(str)
        local temp;
        if string.startWith(str, "(") and string.endWith(str, ")") then
            temp = string.explode(string.sub(str, 2, -2), ",");
            return cc.p(tonumber(temp[1]), tonumber(temp[2]));
        else
            error("bad pos format:" .. str);
        end
    end

    info.idle_effect_pos = parsePosStr(info.idle_effect_pos);
    info.idle_effect_offset = parsePosStr(info.idle_effect_offset);
    info.hit_effect_pos = parsePosStr(info.hit_effect_pos);
    info.hit_effect_offset = parsePosStr(info.hit_effect_offset);
    info.disappear_effect_pos = parsePosStr(info.disappear_effect_pos);
    info.disappear_effect_offset = parsePosStr(info.disappear_effect_offset);
    info.disappear_frame = parsePosStr(info.disappear_frame);
    info.click_size = parsePosStr(info.click_size);
    return info;
end

-- 检索彩蛋信息
function query(id, path)
    local m = eggsTable[id]

    if not m or path == "" or not path then
        return m
    end

    return m[path]
end

-- 更新当前彩蛋信息
function updateEggsInfo(info)
    eggsInfo = info;

    EventMgr.fire(event.EGGS_INFO_UPDATED);
end

-- 获取彩蛋信息
function getEggsList()
    if eggsInfo == nil or eggsInfo.egg_list == nil then
        return {};
    end

    return eggsInfo.egg_list;
end


